#include "test_unzip.h"
#include "mytest_api.h"
#include "mytest_log.h"

void unzip_zip(_tcstring & src_zip, _tcstring & dst_dir)
{
    if (src_zip.empty()) return;

    _tstring file_name = src_zip.substr(src_zip.find_last_of("/") + 1);
    if (file_name.empty()) return;

    LOG_DEBUG("file name : [%s]", file_name.c_str());

    _tstring dstdir_name = dst_dir + "/" + file_name.substr(0, file_name.find_last_of("."));
    LOG_DEBUG("dstdir_name : [%s]", dstdir_name.c_str());

    _tstring stm = getLocTime();
    if (0 == is_dir_exist(dstdir_name.c_str())) {
        char zip[64];
        std::sprintf(zip, "zip -r %s/%s_%s %s", dst_dir.c_str(), stm.c_str(), file_name.c_str(), dstdir_name.c_str());
        system(zip);
        LOG_DEBUG("%s", zip);

        LOG_DEBUG("[%s] exist !~", dstdir_name.c_str());
        char mv[64];
        std::sprintf(mv, "rm -rf %s", dstdir_name.c_str());
        system(mv);
    }

    char unzip[64];
    std::sprintf(unzip, "unzip %s -d %s", src_zip.c_str(), dst_dir.c_str());
    LOG_DEBUG("%s", unzip);
    system(unzip);

    char rm[64];
    std::sprintf(rm, "rm %s/*.zip", dst_dir.c_str());
    system(rm);
}


void ReadConfigFile(_tcstring &path, StAppInfo &st)
{
    FILE *fp = fopen(path.c_str(), "r");
    if(NULL == fp) {
        LOG_ERROR("failed to open [%s]", path.c_str());
        return ;
    }

    char szTest[1024] = {0};

    while (!feof(fp)) {
        memset(szTest, 0, sizeof(szTest));
        fgets(szTest, sizeof(szTest) - 1, fp);
        char *c_tmp = NULL;
        if (c_tmp = strstr(szTest, "")) *c_tmp = '\0';
        if (c_tmp = strstr(szTest, "\r")) *c_tmp = '\0';

        _tstring tmp(szTest);
        if (tmp.find("name") != _tstring::npos) {
            st.strName = tmp.substr(tmp.find_last_of("=") + 1);
        }

        if (tmp.find("version") != _tstring::npos) {
            st.strVersion = tmp.substr(tmp.find_last_of("=") + 1);
        }

        if (!st.strName.empty() || !st.strVersion.empty()) {
            st.bQnx = true;
        }
    }

    fclose(fp);
}

void SearchQNXDir(_tcstring & dirname, std::vector<StAppInfo> & vct, const bool & bInit)
{
    if (dirname.empty()) return;

    _tstring strpath(dirname);
    if ((dirname.find_last_of("/") + 1) != strlen(dirname.c_str())) {
        strpath = dirname + "/";
    }
    LOG_DEBUG("strpath [%s] !", strpath.c_str());

    DIR *pdir = opendir(strpath.c_str());
    if (NULL == pdir) {
        LOG_ERROR("cannot open path [%s]", strpath.c_str());
        return;
    }

    struct dirent *pd;
    struct stat sbuf;
    chdir(strpath.c_str());

    while ((pd = readdir(pdir)) != NULL) {
        lstat(pd->d_name, &sbuf);

        if (strcmp(pd->d_name, ".") == 0 || strcmp(pd->d_name, "..") == 0) {
            continue;
        }

        if(S_ISDIR(sbuf.st_mode)) {
            _tstring path = strpath + pd->d_name + "/";
            SearchQNXDir(path, vct);
        }
        else {
            _tstring name(pd->d_name);
            if (name.find(".config") != _tstring::npos && !bInit) {
                StAppInfo st;
                _tstring fullpath = strpath + name;
                ReadConfigFile(fullpath, st);
                vct.push_back(st);
            }
            else if (name.find(".zip") != _tstring::npos && bInit) {
                /// TODO
                ///
                ///
                ///
            }
        }
    }

    chdir("..");
    closedir(pdir);
}
